﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

namespace Web.Shalo.BanPMS1.Admin
{
    public partial class QLNhaCC : System.Web.UI.Page
    {
        string mancc = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    DSNhaCC();
                    MultiView1.ActiveViewIndex = 0;
                }
            }
        }

        public void DSNhaCC()
        {
            NhaccController obj = new NhaccController();
            GVNhacc.DataSource = obj.bllDSNhacc(mancc);
            GVNhacc.DataBind();
        }

        protected void GVNhacc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVNhacc.PageIndex = e.NewPageIndex;
            DSNhaCC();
        }

        protected void btnTaomoi_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            btnSuaNCC.Visible = false;

        }

        protected void btnThemNCC_Click(object sender, EventArgs e)
        {
            NhaccController obj = new NhaccController();
            bool res = obj.bllThemNhacc(txtTenncc.Text, txtDiachi.Text, txtEmail.Text, txtSdt.Text);
            ThongBao(res);
        }

        public void ThongBao(bool res)
        {
            if (res)
            {
                Response.Redirect("QLNhaCC.aspx");
            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }
    
        }

        public void HienThi(string mancc)
        {
            NhaccController obj = new NhaccController();
            DataTable NCC = obj.bllDSNhacc(mancc);
            txtTenncc.Text = NCC.Rows[0]["TenNCC"].ToString();
            txtDiachi.Text=NCC.Rows[0]["DiaChi"].ToString();
            txtEmail.Text = NCC.Rows[0]["Email"].ToString();
            txtSdt.Text = NCC.Rows[0]["SDT"].ToString();
            labMaNCC.Text=NCC.Rows[0]["MaNCC"].ToString();
        }

        protected void GVNhacc_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            NhaccController obj = new NhaccController();
            MultiView1.ActiveViewIndex = 1;
            btnThemNCC.Visible = false;
            mancc = GVNhacc.Rows[e.NewSelectedIndex].Cells[0].Text;
            HienThi(mancc);
        }

        protected void btnSuaNCC_Click(object sender, EventArgs e)
        {
            bool res = false;  
            NhaccController obj=new NhaccController();
            res = obj.bllSuaNhacc(txtTenncc.Text, txtDiachi.Text, txtEmail.Text, txtSdt.Text, labMaNCC.Text);
            ThongBao(res);
        }

        protected void bthHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("QLNhaCC.aspx");
        }

        protected void GVNhacc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool res = false;
            NhaccController obj = new NhaccController();
            mancc = GVNhacc.Rows[e.RowIndex].Cells[0].Text;
            res = obj.bllXoaNhacc(mancc);
            ThongBao(res);
        }

    }
}